<?php defined('SYSPATH') OR die('No direct access allowed.');

class Song_Controller extends Master_Admin_Controller{

	public function index(){
		$this->page(1);
	}

	public function page($pagenum=1)
	{
		$view = new View('admin/song');

		$this->set_title('ผู้ดูแลระบบ - เพลง');
			
		$songs = new Songs_Model;

		// Instantiate Pagination, passing it the total number of product rows.
		$paging = new Pagination(array
		(
			'base_url'    => 'admin/song/ajax_page/',
			'uri_segment' => 4,
			'total_items' => $songs->count_songs(),
		));

		// Render the page links
		$view->pagination = $paging->render();

		// Display X items per page, from offset = page number
		//		$view->songs = $songs->browse($paging->items_per_page, $paging->sql_offset);
		$view->songs = $songs->get_song_show($paging->items_per_page, $paging->sql_offset);

		$this->template->content = $view;
		$this->template->render(TRUE);

	}

	public function ajax_page($pagenum=1)
	{
		if(request::is_ajax())
		{
			$this->auto_render=false;
			$ajax = new ISS_Ajax();

			$view = new View('admin/song');
			$song = new Songs_Model;

			// Instantiate Pagination, passing it the total number of product rows.
			$paging = new Pagination(array
			(
				'base_url'    => 'admin/song/ajax_page/',
				'uri_segment' => 4,
				'total_items' => $song->count_songs(),
			));

			// Render the page links
			$view->pagination = $paging->render();

			// Display X items per page, from offset = page number
			$view->songs = $song->browse($paging->items_per_page, $paging->sql_offset);
			$selector = "mainCont";
			$content = $view;
			$ajax->addViewUpdate($selector, $content);
			echo json_encode($ajax->getHtmlAjax());
		}
		else {
			$this->page($pagenum);
		}
	}

	public function add($status='') {
		$view = new View('admin/song_form');

		$this->set_title('ผู้ดูแลระบบ - สร้างเพลง');

		$form = array (
				'id' => '',
				'title'    => '',
				'album'    => '',
				'gene'    => '',
				'track_no' => '',
				'duration' => '',
				'sample_file'   => '',
				'low_bit_rate_file' => '',
				'high_bit_rate_file' => '',
		);

		$view->mode = 'add';
		$view->status = $status;
		if ($status != '') {
			$form['title'] = (isset($_POST['txtTitle']) ? $_POST['txtTitle'] : '');
			$form['album'] = (isset($_POST['cboAlbum']) ? $_POST['cboAlbum'] : '');
			$form['gene'] = (isset($_POST['cboGene']) ? $_POST['cboGene'] : '');
			$form['track_no'] = (isset($_POST['txtTreckNo']) ? $_POST['txtTreckNo'] : '');
			$form['duration'] = (isset($_POST['txtDuration']) ? $_POST['txtDuration'] : '');
			$form['sample_file'] = (isset($_POST['txtSampleFile']) ? $_POST['txtSampleFile'] : '');
			$form['low_bit_rate_file'] = (isset($_POST['txtLowBit']) ? $_POST['txtLowBit'] : '');
			$form['high_bit_rate_file'] = (isset($_POST['txtHingBit']) ? $_POST['txtHingBit'] : '');
		}
		$view->form = $form;
		//gens model
		$genes = new Genes_Model;
		$view->genes = $genes->loads();
		//albume model
		$albums = new Albums_Model;		
		$view->albums = $albums->loads();
		//artist
		$artists =new Artists_Model();
		$view->artists = $artists->loads();

		$view->errors = array();
		$this->template->content = $view;
		$this->template->render(TRUE);
	}

	public function edit($id, $status='') {
		$view = new View('admin/club_form');

		$this->set_title('ผู้ดูแลระบบ - แก้ไขเพลง');
		$songs = new Songs_Model;

		$view->mode = 'edit';
		$view->status = $status;
		if ($status != '') {
			$form['title'] = (isset($_POST['txtTitle']) ? $_POST['txtTitle'] : '');
			$form['album'] = (isset($_POST['cboAlbum']) ? $_POST['cboAlbum'] : '');
			$form['gene'] = (isset($_POST['cboGene']) ? $_POST['cboGene'] : '');
			$form['track_no'] = (isset($_POST['txtTreckNo']) ? $_POST['txtTreckNo'] : '');
			$form['sample_file'] = (isset($_POST['txtSampleFile']) ? $_POST['txtSampleFile'] : '');
			$form['low_bit_rate_file'] = (isset($_POST['txtLowBit']) ? $_POST['txtLowBit'] : '');
			$form['high_bit_rate_file'] = (isset($_POST['txtHingBit']) ? $_POST['txtHingBit'] : '');
			 
			$view->form = $form;
		}
		else {
				
			$forms = $songs->load((isset($_POST['cboAlbum']) ? $_POST['cboAlbum'] : ''),(isset($_POST['txtTreckNo']) ? $_POST['txtTreckNo'] : ''));
				
			foreach ($forms as $row)
			{
				$view->form = $row;
			}
		}
		$view->errors = array();
		$this->template->content = $view;
		$this->template->render(TRUE);
	}

	public function save() {
		if($_POST) {
			$post = new Validation($_POST);
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txtTitle','required', 'length[2,255]');
			//			$post->add_rules('txtReleasedDate','required','valid::date');
			$post->post_filter('ucfirst', 'name');

			$form = array (
        		'gene_id' => '',
				'artist_id' => '',
				'album_id' => '',
				'title'    => '',
				'track_no' => '',
				'duration' => '',
				'sample_file'   => '',
				'low_bit_rate_file' => '',
				'high_bit_rate_file' => '',
			);

			if (!$post->validate())
			{
				$errors = $post->errors('song_error');
				$view = new View('admin/song_form');

				$view->errors = $errors;
				$form['title'] = (isset($_POST['txtTitle']) ? $_POST['txtTitle'] : '');
				$form['track_no'] = (isset($_POST['txtTreckNo']) ? $_POST['txtTreckNo'] : '');
				$form['sample_file'] = (isset($_POST['txtSampleFile']) ? $_POST['txtSampleFile'] : '');
				$form['low_bit_rate_file'] = (isset($_POST['txtLowBit']) ? $_POST['txtLowBit'] : '');
				$form['high_bit_rate_file'] = (isset($_POST['txtHingBit']) ? $_POST['txtHingBit'] : '');

				$view->form = $form;
				//gens model
				$genes = new Genes_Model;
				$view->genes = $genes->loads();
				//albume model
				$albums = new Albums_Model;
				$view->albums = $albums->loads();
				$this->set_title('ผู้ดูแลระบบ - สร้างเพลง');
				$view->mode = 'add';
				$this->template->content = $view;
				$this->template->render(TRUE);
			}
			else {
				$songs = new Songs_Model;

				$songs->file_validate('txtSampleFile');
				$sampleFile = $songs->get_filename();

				$songs->file_validate('txtLowBit');
				$lowBitFile = $songs->get_filename();

				$songs->file_validate('txtHingBit');
				$highBitFile = $songs->get_filename();

				if ($_POST['mode']=='edit') {
					$form['id'] = $_POST['id'];
				}
				$form['title'] = $_POST['txtTitle'];
				$form['artist_id'] = $_POST['cboArtist'];
				$form['gene_id'] = $_POST['cboGene'];
				$form['album_id'] = $_POST['cboAlbum'];
				$form['track_no'] = $_POST['txtTreckNo'];
				$form['duration'] = $_POST['txtDuration'];
				$form['sample_file'] = (isset($_POST['txtSampleFile']) ? $_POST['txtSampleFile'] : '');
				$form['low_bit_rate_file'] = (isset($_POST['txtLowBit']) ? $_POST['txtLowBit'] : '');
				$form['high_bit_rate_file'] = (isset($_POST['txtHingBit']) ? $_POST['txtHingBit'] : '');

				if (!empty($sampleFile)) {
					$form['sample_file'] = str_replace(DOCROOT, '', $sampleFile);
				}
				if (!empty($lowBitFile)) {
					$form['low_bit_rate_file'] = str_replace(DOCROOT, '', $lowBitFile);
				}
				if (!empty($highBitFile)) {
					$form['high_bit_rate_file'] = str_replace(DOCROOT, '', $highBitFile);
				}

				if ($songs->save($form, $_POST['mode'])) {
					url::redirect('admin/song');
				}
				else {
					$status = Kohana::lang('song_error.save_result.save_failed');
					if ($_POST['mode']=='add') {
						$this->add($status);
					}
					else {
						$this->edit($_POST['id'], $status);
					}
				}
			}

		}
	}

}